package main

import (
	"github.com/cihub/seelog"
	"github.com/goftp/server"
	"log"
	"myFtp/ca"
	"myFtp/conf"
)

func main() {
	// init Logger
	defer seelog.Flush()
	logger, err := seelog.LoggerFromConfigAsFile("seelog.xml")
	if err != nil {
		panic(err)
	}
	seelog.ReplaceLogger(logger)
	go func() {
		ca.RunFunc()
	}()
	ftpConf := conf.FtpConf()
	if ftpConf.Dir == "" {
		log.Fatalf("Please set a root to serve with -root")
	}

	factory := &ca.FileDriverFactory{
		RootPath: ftpConf.Dir,
		Perm:     server.NewSimplePerm("user", "group"),
	}

	opts := &server.ServerOpts{
		Factory:      factory,
		Port:         ftpConf.Port,
		Hostname:     ftpConf.Host,
		PassivePorts: ftpConf.PassivePorts,
		Auth:         &server.SimpleAuth{Name: ftpConf.User, Password: ftpConf.Pass},
	}

	seelog.Infof("Starting ftp server on %v:%v", opts.Hostname, opts.Port)
	seelog.Infof("Username %v, Password %v", ftpConf.User, ftpConf.Pass)
	server := server.NewServer(opts)
	err = server.ListenAndServe()
	if err != nil {
		log.Fatal("Error starting server:", err)
	}
}
